package org.micode.common.syslog;

import org.micode.common.exception.InvalidClassException;
import org.micode.common.utils.ObjectUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;

@Slf4j
public class SysLogSaverFactory {

    static ISysLogSaver<?> defaultSysLogSaver;

    public static ISysLogSaver<?> newSysLogSaver(String implClassName) {
        if (null != defaultSysLogSaver) {
            return defaultSysLogSaver;
        }

        ISysLogSaver<?> ret;
        if (StringUtils.isEmpty(implClassName)) {
            ret = DefaultSysLogSaver.getInstance();
        } else {
           try {
                ret = ObjectUtils.createByClassName(implClassName);
            } catch (Exception e) {
                log.error("Instantiation exception of ISysLogSaver({})", implClassName, e);
                throw new InvalidClassException("Instantiation exception of ISysLogSaver");
            }
        }

        defaultSysLogSaver = ret;

        return defaultSysLogSaver;
    }

}
